#pragma once
#include <deque>
#include <map>
#include <mutex>

using namespace std;

struct Pos
{
	int x_pos;
	int y_pos;
	char id;
};
mutex mtx_player_queue;
mutex mtx_game_queue;

class Player
{
private:
	char up, left, down, right;
	struct Pos pos;
	int size;
	deque<char> *player_queue;
	deque<Pos> *game_queue;

public:

	Player(void)
	{
		up = 0;
		left = 0;
		down = 0;
		right = 0;
		pos.x_pos = 0;
		pos.y_pos = 0;
	}

	Player(char up, char left, char down, char right, char pos_id, 
		int x_pos, int y_pos, int size, deque<char> &player_queue, deque<Pos> &game_queue)
	{
		this->up = up;
		this->left = left;
		this->down = down;
		this->right = right;
		this->pos.id = pos_id;
		this->pos.x_pos = x_pos;
		this->pos.y_pos = y_pos;
		this->player_queue = &player_queue;
		this->game_queue = &game_queue;
		this->size = size;
		mtx_game_queue.lock();
		game_queue.push_back(pos);
		mtx_game_queue.unlock();
	}

	void MovePlayer()
	{
		mtx_player_queue.lock();
		if (!player_queue->empty())
		{
			char c = player_queue->front();
			player_queue->pop_front();

			if( c == up && pos.y_pos > 0)
				pos.y_pos = pos.y_pos - 1;
			else if( c == left && pos.x_pos > 0)
				pos.x_pos = pos.x_pos - 1;
			else if( c == down && pos.y_pos < size - 1)
				pos.y_pos = pos.y_pos + 1;
			else if( c == right && pos.x_pos < size - 1)
				pos.x_pos = pos.x_pos + 1;
			mtx_game_queue.lock();
			game_queue->push_back(pos);
			mtx_game_queue.unlock();
		}
		mtx_player_queue.unlock();
	}

	void operator()()
	{
		while(true)
			MovePlayer();
	}

	~Player(void)
	{
	}
};

